package com.itany.corejava.day17;

import com.itany.corejava.day12.User;

import java.io.BufferedReader;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class ObjectFactory {

    private static Map<String,Object> objects=new HashMap<>();


    static {
        try(BufferedReader br=new BufferedReader(
                new InputStreamReader(
                        ObjectFactory.class
                                     .getClassLoader()
                                     .getResourceAsStream("objects.cfg")
                )
        )){

            String s;

            while((s=br.readLine())!=null){
                String[] entry=s.split("=");

                if("ComponentScan".equals(entry[0])){

                    System.out.println(entry[1]);


                    System.out.println(File.separator);

                    String realPath=ObjectFactory.class
                            .getClassLoader()
                            .getResource(entry[1].replace(".", "/"))
                            .getPath();

                    System.out.println(realPath);

                    File dir=new File(realPath);

                    File[] files=dir.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            return name.endsWith(".class")&&!name.contains("$");
                        }
                    });


                    for (File file : files) {
//                        System.out.println(file.getName());

                        String simpleClassName=file.getName().substring(0,file.getName().lastIndexOf(".class"));

                        String className=entry[1]+"."+simpleClassName;


                        Class objClass=Class.forName(className);

                        Component component= (Component) objClass.getAnnotation(Component.class);

//                        System.out.println(component);


                        if(component!=null){
                            Object obj=objClass.newInstance();

                            String value=component.value();


                            if("".equals(value)){
                                value=simpleClassName.substring(0,1).toLowerCase()+simpleClassName.substring(1);
                            }
                            System.out.println(value);

                            objects.put(value,obj);

                        }


                    }

                    continue;



                }



                objects.put(entry[0],Class.forName(entry[1]).newInstance());
            }


        }catch (Exception e){
            e.printStackTrace();
            throw new ExceptionInInitializerError("ObjectFactory初始化错误");
        }
    }


    public static Object getObject(String key){
        return objects.get(key);
    }

    public static void main(String[] args) {

    }

}
